﻿using FastEndpoints;
using vite_net.Common;
using vite_net.Controllers.WorkFlow;
using vite_services;

namespace vite_net.Controllers.User.UserInfo
{
    public class UserInfoEndpoint : ViteNetEndpoint<EmptyRequest, Result<Response_UserInfo>>
    {
        private readonly UserServices _userServices;
        public UserInfoEndpoint(IConfiguration configuration, UserServices userServices)
        {
            _userServices = userServices;
        }

        public override void Configure()
        {
            Get("/user/info");
            Roles("Manager", "ADMIN");
        }

        public override async Task HandleAsync(EmptyRequest req, CancellationToken ct)
        {
            var user = await _userServices.GetUserByUserIdAsync(GetCurrentUserId());
            // 获取roles
            var roles_Code_Id = await _userServices.GetUserRolesByUserId(user.Id);
            // 获取menus
            var menus = await _userServices.GetUserMenusByRoleIds(roles_Code_Id.Item2);
            // 处理菜单
            var rtMenus = MenuHandler.GenerateMenus(menus);

            var responseData = new Response_UserInfo()
            {
                UserName = user.Username,
                NickName = user.Nickname,
                Avatar = user.Avatar,
                UserId = user.Id,
                Roles = roles_Code_Id.Item1.ToArray(),
                Perms = new string[] { "sys:user:add", "sys:user:delete", "sys:user:password:reset", "sys:user:edit", "sys:user:delete" },
                Menus = rtMenus
            };

            await SendAsync(Result<Response_UserInfo>.Ok(responseData));
        }
    }
}
